package com.ruiboyun.liveapp.ui;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.ruiboyun.liveapp.R;
import com.ruiboyun.liveapp.adapter.ReviewAdapter;
import com.ruiboyun.liveapp.base.BaseActivity;
import com.ruiboyun.liveapp.base.Url;
import com.ruiboyun.liveapp.bean.Review;
import com.ruiboyun.liveapp.bean.ReviewEntity;
import com.ruiboyun.liveapp.bean.Share;
import com.ruiboyun.liveapp.bean.UpdatePortEvent;
import com.ruiboyun.liveapp.dialog.SharePopupWindow;
import com.ruiboyun.liveapp.listener.OnItemClickListener;
import com.ruiboyun.liveapp.presenter.ReviewPresenter;
import com.ruiboyun.liveapp.presenter.impl.ReviewPresenterImpl;
import com.ruiboyun.liveapp.util.tool.EditorShare;
import com.ruiboyun.liveapp.util.tool.ToasUtil;
import com.ruiboyun.liveapp.view.ReviewView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class ReviewActivity extends BaseActivity implements ReviewView, ReviewAdapter.OnReviewListener {


    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    private ReviewPresenter reviewPresenter;
    private String channelId;
    ReviewAdapter reviewAdapter;
    private List<Review> reviewList = new ArrayList<>();


    public static void go(Context context, String channelId) {
        Intent intent = new Intent(context, ReviewActivity.class);
        intent.putExtra("channelId", channelId);
        context.startActivity(intent);
    }

    SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            stopRefresh(refreshLayout);
            String string = EditorShare.getString(Url.PORT);
            reviewPresenter.getReview(string, channelId);
        }
    };


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UpdatePortEvent event) {
        refreshLayout.setRefreshing(true);
        listener.onRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_review;
    }

    @Override
    protected void afterCreate(@Nullable Bundle savedInstanceState) {
        channelId = getIntent().getStringExtra("channelId");
        reviewAdapter = new ReviewAdapter(this, reviewList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(reviewAdapter);
        reviewPresenter = new ReviewPresenterImpl(this);
        EventBus.getDefault().register(this);
        refreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.blue_login));
        refreshLayout.setOnRefreshListener(listener);
        refreshLayout.setRefreshing(true);
        listener.onRefresh();
        reviewAdapter.setOnReviewListener(this);


    }

    @OnClick({R.id.back})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                removeStack(this);
                break;

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void getReviewSuccess(ReviewEntity reviewEntity) {
        reviewList.clear();
        if (reviewEntity.getReviews() != null) {
            reviewList.addAll(reviewEntity.getReviews());
        }

        reviewAdapter.notifyDataSetChanged();
    }

    @Override
    public void getReviewError(int errorCode) {

    }

    @Override
    public void showTips(String msg) {
        ToasUtil.showCenterToast(msg);
    }


    @Override
    public void onReview(Review review) {
        WebActivity.go(this, JSON.toJSONString(review));
    }

    @Override
    public void onShare(View view, Review review) {
        Share share = new Share();
        share.setTitle(review.getTitle());
        share.setContent(review.getDesc());
        share.setUrl(review.getPlay_addr());
        share.setLogo(review.getCover_addr());
        SharePopupWindow sharePopupWindow = new SharePopupWindow(this, share);
        sharePopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }
}
